package cn.gov.pbc.dream.controller;

import cn.gov.pbc.dream.common.Const;
import cn.gov.pbc.dream.common.ResponseCode;
import cn.gov.pbc.dream.common.ServerResponse;
import cn.gov.pbc.dream.pojo.UserSession;
import cn.gov.pbc.dream.pojo.dto.user.bankfinancing.org.AddOrgDto;
import cn.gov.pbc.dream.pojo.dto.user.bankfinancing.org.UpdateOrgNameDto;
import cn.gov.pbc.dream.pojo.query.bankfinancing.OrgQuery;
import cn.gov.pbc.dream.service.IBankFinancingOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/bank-financing/")
public class BankFinancingOrgController {

    @Autowired
    private IBankFinancingOrgService iBankFinancingOrgService;

    @PostMapping("getOrgList.do")
    public ServerResponse getOrgList(HttpSession session, @RequestBody OrgQuery orgQuery){
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iBankFinancingOrgService.getOrgList(orgQuery);
    }

    @PostMapping("updateOrgName.do")
    public ServerResponse updateFinancingOrgName(HttpSession session, @RequestBody UpdateOrgNameDto updateFinancingOrgNameDto){
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iBankFinancingOrgService.updateOrgName(updateFinancingOrgNameDto);
    }

    @DeleteMapping("deleteOrg.do/{id}")
    public ServerResponse deleteFinancingOrg(HttpSession session, @PathVariable Integer id){
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iBankFinancingOrgService.deleteOrg(id);
    }

    @PostMapping("addOrg.do")
    public ServerResponse addFinancingOrg(HttpSession session, @RequestBody AddOrgDto addOrgDto){
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iBankFinancingOrgService.addOrg(addOrgDto);
    }
}
